//
//  RiddingViewController.m
//  Ridding
//
//  Created by zys on 12-3-19.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "RiddingViewController.h"
#import "ActivityView.h"
#import "RiddingAppDelegate.h"
#import <QuartzCore/QuartzCore.h>
#import "ResponseCodeCheck.h"
@implementation RiddingViewController
@synthesize request;
@synthesize requestUtil;
@synthesize staticInfo;
@synthesize homeViewController;
@synthesize bgImageView=_bgImageView;
//点击新浪微博
-(IBAction)clickSinaWeibo:(id)sender{
    SourceLoginViewController *loginController=[[SourceLoginViewController alloc]init];
    [self.navigationController pushViewController:loginController animated:YES];
}



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    request=[SinaApiRequestUtil getSinglton];
    requestUtil=[RequestUtil getSinglton];
    staticInfo=[StaticInfo getSinglton];
    //检查网络
    [[ResponseCodeCheck getSinglton] checkConnect];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    staticInfo.user.userId = [prefs stringForKey:@"userId"];
    staticInfo.user.authToken= [SFHFKeychainUtils getPasswordForUsername:staticInfo.user.userId andServiceName:@"riddingapp" error:nil];
    staticInfo.user.accessUserId = [prefs stringForKey:@"accessUserId"];
    staticInfo.user.accessToken=[prefs stringForKey:@"accessToken"];
    staticInfo.user.sourceType = [prefs integerForKey:@"sourceType"];
    RiddingAppDelegate *delegate=(RiddingAppDelegate *)[[UIApplication sharedApplication] delegate];
    //如果新浪成功，并且authtoken有效
    if([delegate canLogin]){
        homeViewController = [HomeViewController alloc];
        [self.navigationController pushViewController:homeViewController animated:YES];
        staticInfo.logined=true;
        return;
    }
    NSLog(@"authToken From prefs: %@", staticInfo.user.authToken);
    [super viewDidLoad];
    
  }


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    int random = (arc4random() % 3) + 1;
    NSString *imageName = [NSString stringWithFormat:@"B%d.png",random];
    UIImage *bg = [UIImage imageNamed:imageName];
    self.bgImageView.image=bg;
    
    
    UIView *sloganBG = [[UIView alloc] initWithFrame:CGRectMake(-10, 40, 230, 120)];
    [sloganBG setBackgroundColor:[UIColor blackColor]];
    [sloganBG setAlpha:0.86];
    sloganBG.layer.cornerRadius = 10;
    [self.view addSubview:sloganBG];
    
    UIImageView *slogan = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 200, 100)];
    UIImage *sl = [UIImage imageNamed:@"slogan.png"];
    slogan.image = sl;
    [self.view addSubview:slogan];

    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown);
}

@end
